Geographic map (U.S., world, continent) that shows how a variable is geographically distributed.
R has great packages like tigris (for U.S.) and rnaturalearth (for world maps).
Install and load the packages at the top of the R script.
Start simple: Use tigris to get a national map with state boundaries (take out PR)
This code will use tigris to create a data object stored in your environment. Note that adding “shift_geometry” is a key command for putting Alaska and Hawaii on the bottom left.
ggplot(stv20, aes(fill = called)) +
geom_sf(color = "gray90", linewidth=.2) +
scale_fill_manual(values = c("blue", "red"), labels=c("Biden", "Trump")) +
theme_map() + coord_sf(expand=FALSE) +
labs(fill = "",
title = " 2020 US presidential election results by state",
caption = "Note: Nebraska and Maine split electoral college votes by congressional district") +
theme(plot.title=element_text(face="bold", size=12, hjust=.5),
plot.caption = element_text(size=10),
legend.text = element_text(size = 8))w |>
filter(type != "Dependency", type !="Disputed", type != "Indeterminate") |>
ggplot() + geom_sf(aes(fill=income_grp), color="black", linewidth=.1) +
scale_fill_brewer(palette = "YlGnBu", direction=-1,
labels=c("High OECD", "High non-OECD", "Upper middle", "Lower middle", "Low")) +
coord_sf(crs='ESRI:54030', expand=FALSE) +
labs(fill = "Income Level", title = "World Map, Country Income Levels",
caption="Source: R Natural Earth Data") +
theme_map() +
theme(plot.title=element_text(face="bold", size=12, hjust=.5),
plot.caption = element_text(size=10),
legend.title = element_text(size=10),
legend.text = element_text(size = 8))vw |>
filter(type != "Dependency", type !="Disputed", type != "Indeterminate") |>
ggplot() +
geom_sf(color="black", linewidth=0.1, aes(fill = as.factor(v2jupoatck_ord))) +
coord_sf(crs='ESRI:54030', expand=FALSE) +
scale_fill_brewer(type = "seq", palette = "YlGnBu", na.translate=FALSE,
labels=c("Daily/weekly", "Every month", "More than once", "Rare", "None"),
direction = -1) +
labs(fill = "Attacks", title = "Government Attacks on the Judiciary, 2021",
subtitle="Based on VDem's v2jupoatck_ord Variable",
caption="Source: VDem") +
theme_map() +
theme(plot.title=element_text(face="bold", size=15, hjust=.5),
plot.subtitle=element_text(size=12, hjust=.5),
plot.caption = element_text(size=10),
legend.text = element_text(size = 10),
legend.title = element_text(size = 10) )